package com.csu.cphone.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.csu.cphone.dao.MaintainProblemMapper;
import com.csu.cphone.dao.MaintainProblemSelectMapper;
import com.csu.cphone.pojo.MaintainProblemPojo;
import com.csu.cphone.pojo.MaintainProblemSelectPojo;
import com.csu.cphone.service.MaintainProblemSelectService;
import com.csu.cphone.service.MaintainProblemService;

/**
 * @项目名称：cphone
 * @类名称：MaintainProblemServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月7日 上午9:48:07
 * @company:
 */
@Service
public class MaintainProblemServiceImpl implements MaintainProblemService
{

    @Autowired
    private MaintainProblemMapper maintainProblemMapper;

    @Autowired
    private MaintainProblemSelectMapper maintainProblemSelectMapper;

    @Autowired
    private MaintainProblemSelectService maintainProblemSelectService;

    /**
     * @see com.csu.cphone.service.MaintainProblemService#add(java.lang.Integer,
     *      java.lang.String, java.lang.Integer, java.util.List)
     */
    @Override
    public MaintainProblemPojo add(Integer phoneId, String problemName, Integer problemType,
            List<MaintainProblemSelectPojo> selects)
    {
        MaintainProblemPojo maintainProblem = new MaintainProblemPojo();

        maintainProblem.setPhoneId(phoneId);
        maintainProblem.setProblemName(problemName);
        maintainProblem.setProblemType(problemType);
        maintainProblemMapper.insert(maintainProblem);

        if (!CollectionUtils.isEmpty(selects))
        {
            for (MaintainProblemSelectPojo select : selects)
            {
                select.setProblemId(maintainProblem.getId());
            }
            maintainProblemSelectMapper.batchInsert(selects);
        }
        return maintainProblem;
    }

    /**
     * @see com.csu.cphone.service.MaintainProblemService#update(com.csu.cphone.pojo.MaintainProblemPojo)
     */
    @Override
    public Integer update(Integer id, Integer phoneId, String problemName, Integer problemType,
            List<MaintainProblemSelectPojo> selects)
    {
        MaintainProblemPojo maintainProblem = new MaintainProblemPojo();
        maintainProblem.setId(id);
        maintainProblem.setPhoneId(phoneId);
        maintainProblem.setProblemName(problemName);
        maintainProblem.setProblemType(problemType);
        if (!CollectionUtils.isEmpty(selects))
        {
            for (MaintainProblemSelectPojo select : selects)
            {
                Integer selectId = select.getId();
                if (selectId != null && selectId != 0)
                {

                    maintainProblemSelectService.update(select);
                }
                else
                {
                    maintainProblemSelectService.add(select.getProblemId(), select.getProblemItem(), select.getCost());
                }

            }
        }
        return maintainProblemMapper.update(maintainProblem);
    }

    /**
     * @see com.csu.cphone.service.MaintainProblemService#queryById(java.lang.Integer)
     */
    @Override
    public MaintainProblemPojo queryById(Integer maintainProblemId)
    {
        MaintainProblemPojo maintainProblem = maintainProblemMapper.selectById(maintainProblemId);
        List<MaintainProblemSelectPojo> selects = maintainProblemSelectMapper.selectByProblemId(maintainProblemId);
        maintainProblem.setSelects(selects);
        return maintainProblem;
    }

    /**
     * @see com.csu.cphone.service.MaintainProblemService#count()
     */
    @Override
    public Integer count(Integer phoneId)
    {
        if (phoneId != null && phoneId == 0)
        {
            phoneId = null;
        }
        return maintainProblemMapper.count(phoneId);
    }

    /**
     * @see com.csu.cphone.service.MaintainProblemService#queryList(java.lang.Integer,
     *      java.lang.Integer, java.lang.String)
     */
    @Override
    public List<MaintainProblemPojo> queryList(Integer pageSize, Integer startIndex, String order,
            Integer maintainPhoneId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("startIndex", startIndex);
        map.put("order", order);
        if (maintainPhoneId != null && maintainPhoneId != 0)
        {
            map.put("phoneId", maintainPhoneId);
        }

        List<Integer> maintainProblemIds = new ArrayList<Integer>();
        List<MaintainProblemPojo> maintainProblemList = maintainProblemMapper.queryList(map);

        if (CollectionUtils.isEmpty(maintainProblemList))
        {
            return maintainProblemList;
        }
        for (MaintainProblemPojo maintainProblem : maintainProblemList)
        {
            maintainProblemIds.add(maintainProblem.getId());
        }
        List<MaintainProblemSelectPojo> selects = maintainProblemSelectMapper.selectByProblemIds(maintainProblemIds);
        Map<Integer, List<MaintainProblemSelectPojo>> selectMap = new HashMap<Integer, List<MaintainProblemSelectPojo>>();
        for (MaintainProblemSelectPojo select : selects)
        {
            Integer problemId = select.getProblemId();
            List<MaintainProblemSelectPojo> thisProblemSelects = selectMap.get(problemId);
            if (thisProblemSelects == null)
            {
                thisProblemSelects = new ArrayList<MaintainProblemSelectPojo>();
            }
            thisProblemSelects.add(select);
            selectMap.put(problemId, thisProblemSelects);
        }

        for (MaintainProblemPojo problem : maintainProblemList)
        {
            Integer problemId = problem.getId();
            problem.setSelects(selectMap.get(problemId));
        }

        return maintainProblemList;
    }

    /**
     * @see com.csu.cphone.service.MaintainProblemService#deleteById(java.lang.Integer)
     */
    @Override
    public Integer deleteById(Integer maintainProblemId)
    {
        return maintainProblemMapper.deleteById(maintainProblemId);
    }

    /**
     * @see com.csu.cphone.service.MaintainProblemService#batchAdd(java.lang.Integer,
     *      java.util.List)
     */
    @Override
    public List<MaintainProblemPojo> batchAdd(Integer phoneId, List<MaintainProblemPojo> problems)
    {
        List<MaintainProblemPojo> result = new ArrayList<MaintainProblemPojo>();
        if (!CollectionUtils.isEmpty(problems))
        {
            for (MaintainProblemPojo problem : problems)
            {
                MaintainProblemPojo problemPojo = add(phoneId, problem.getProblemName(), problem.getProblemType(),
                        problem.getSelects());
                result.add(problemPojo);
            }
        }
        return result;
    }

    /**
     * @see com.csu.cphone.service.MaintainProblemService#batchUpdate(java.lang.Integer,
     *      java.util.List)
     */
    @Override
    public Integer batchUpdate(Integer phoneId, List<MaintainProblemPojo> problems)
    {
        int result = 0;
        if (!CollectionUtils.isEmpty(problems))
        {
            for (MaintainProblemPojo problem : problems)
            {
                result += update(problem.getId(), phoneId, problem.getProblemName(), problem.getProblemType(),
                        problem.getSelects());
            }
        }
        return result;
    }

}
